import { INTERACTION_CLICK_DATA } from '@/store/state'
import { PAGE_TYPE, PAGE_TYPE_, CONTROL_TYPES } from '@/common/const'
import { COMPONENT_TYPES } from '@/common/const-wb'
import { H5_TYPE, MOBILE_WID_HEI } from '@/common/const-dif'
import { GENERAL_TYPES_MAP } from '@/common/const-general'
import { STATISTIC_DATA_TYPES_MAP } from '@/common/events'
import { getQueryString } from '@/plugins/utils'
import { computeVoteTotal, resetVisibleComponents, refreshBitmapTextFrame, sortReposts, resetAllCropImgUrl, dealStatisticData, initNumberDataProcess } from '@/store/index-utils'
import { dealPageStyleAndAttr, dealPageOrGroup, dealSiteType, setCompnentDataIsVisible } from '@/store/index-page-data'
import { postStatics } from '@/service/statics'
import { EventBus } from '@/store/index'
import { isHasOwnProperty, getCompIdByParam } from '@/plugins/utils'


export default {
  updateReloadData(state, isReload){
    state.siteInfo.isReload = isReload;
  },
  updatePopReloadData(state, isPopReload){
    state.siteInfo.isPopReload = isPopReload;
  },
  setEnvInfo(state, env){
    state.siteInfo.env = env;
  },
  updateIsMobileInfo(state, md){
    _.merge(state.siteInfo.md, md)
  },
  updateEndSelectScoreFormValues(state, data){
    state.interactionData.endScoreFormValues = data;
  },
  updateFrontSelectScoreFormValues(state, data){
    state.frontSelectScoreFormValues = data;
  },
  setIsTemp(state, isTemp){
    state.siteInfo.isTemp = isTemp;
  },
  updatePageAttr(state, page){
    let currentPage = state.allPagesMap[page.id];
    _.merge(currentPage, page);
  },
  updateComponentAttrUrl(state, data){
    let componentData = state.componentMap[data.id];
    componentData.commonAttr.url_ = componentData.commonAttr.url;
    _.merge(componentData, data);
  },
  updateComponentAttr(state, data){
    let componentData = state.componentMap[data.id];
    _.merge(componentData, data);
  },
  updatePageCropRecoverUrl(state, {id, pageId}){
    if(state.coverPageCropCompAttr[pageId]){
      let arr = state.coverPageCropCompAttr[pageId];
      if(arr.indexOf(id) == -1){
        arr.push(id);
      }
    }else{
      state.coverPageCropCompAttr[pageId] = [id]
    }
  },
  replaceComponentAttr(state, data){
    let componentData = state.componentMap[data.id];
    _.forEach(data, (value, key) => {
      componentData[key] = value;
      if(_.isArray(value)){
        _.forEach(value, (item) => {
          item.id = 'click_item_' + _.random(0, 1000);
        })
      }
    })
  },
  updateFormIsSubmit(state, isSubmit){
    state.siteInfo.formIsSubmit = isSubmit
  },
  updateFrontData(state, {id, data}){
    state.frontData[id] = data
  },
  updateIsTimerEnd(state, {isTimerEnd}){
    state.timeObj.isTimerEnd = isTimerEnd
  },
  updateIsTimerStart(state, {isTimerStart}){
    state.timeObj.isTimerStart = isTimerStart
  },
  updateLoading(state, {isLoading}){
    state.siteInfo.isLoading = isLoading
  },
  updateDisplayAssetsLoading(state, {isDisplayAssetsLoading}){
    state.siteInfo.isDisplayAssetsLoading = isDisplayAssetsLoading
  },
  updatePageOrPopComNum(state, {pageOrPopComNum}){
    state.siteInfo.pageOrPopComNum = pageOrPopComNum
    if(state.siteInfo.pageOrPopComNum <= 0){
      state.siteInfo.isLoading = false
      if(state.popId > 0){
        EventBus.$emit('showPopNoneLoading', {popId: state.popId})
      }
      let pageId = state.pageIndex + 1
      if(state.attrs.h5Type == H5_TYPE.mobile && !state.attrs.isNoSwiping && state.attrs.pageType == PAGE_TYPE.single){
        EventBus.$emit("swiperNoneLoading", {pageId})
      }else{
        EventBus.$emit('pauseVideo', {pageId})
        EventBus.$emit('showPageNoneLoading', {pageId})
      }
    }
  },
  updateTime(state, {id, controlId, data}){
    state.timeObj.timerTime = state.controls[id][controlId].data.time = data.end
    state.timeObj.serverTime = data.current
  },
  updateRepostsDelayTimeTime(state){
    state.siteInfo.repostsDelayTime += 100
  },
  updateGlobalPost(state, data){
    _.merge(state.globalIsPost, data)
  },
  setSiteFrom(state, from){
    state.siteInfo.from = from
  },
  updatePushPicItem(state, {id, controlId, picItem, fileName}){
    const elements = state.controls[id][controlId].data.elements;
    const compData = state.componentMap[id];
    const controlData = state.controls[id][controlId];
    const relateCompId = getCompIdByParam(controlData.data.relateCompId)
    const relateCompData = state.componentMap[relateCompId];
    let itemIndex = 0;
    if(controlData.data.isGetOther){
      if(relateCompId && relateCompData && relateCompData.commonAttr.itemIndex >= 0){
        itemIndex = relateCompData.commonAttr.itemIndex;
      }
    }else{
      //其他动作赋的值 比如发博设置的pushIndex 或者是这个组件的3d旋转设置的
      itemIndex = compData.commonAttr.pushIndex || compData.commonAttr.itemIndex || 0;
    }
    //有可能是-1，需要重置下值
    if(itemIndex < 0){
      itemIndex = 0
    }
    if(fileName) {
      let data = state.controls[id][controlId].data;
      if(!data.pics_) { data.pics_ = {} };
      data.pics_[fileName] = picItem;
    }
    elements[itemIndex].pics.push(picItem);
    // state.coverPushCompPic[id] = id;
  },
  deletePushPicItem(state, {id, controlId, fileNames}){
    const pics_ = state.controls[id][controlId].data.pics_;
    let newPics = [];
    _.forEach(fileNames, (fileName) => {
      newPics.push(pics_[fileName]);
    })
    state.controls[id][controlId].data.elements[0].pics = newPics;
  },
  updateFormrandlist(state, {id, controlId, formList}) {
    formList = _.map(formList, (item) => {
      if(isHasOwnProperty(item, 'sex') && state.bindData.sexIconUrls) {
        if(item.sex == "男") {
          item.sexIconUrl = state.bindData.sexIconUrls[0]
        } else {
          item.sexIconUrl = state.bindData.sexIconUrls[1]
        }
      }
      return item
    })
    if(!state.controls[id][controlId].data.elements) {
      state.controls[id][controlId].data.elements = [];
    }
    state.controls[id][controlId].data.elements = formList;
    EventBus.$emit("refreshDynamicData")
  },
  updatePuzzle(state, {id, controlId, data}){
    //1882 1229非人哉
    if(!_.isArray(data) && data.ext_1){
      let puzzleStr = data.ext_1;
      _.merge(state.controls[id][controlId].data, data);
      state.interactionData.puzzle = puzzleStr.split(',');
    }
    let componentData = state.componentMap[id];
    if(componentData.interactionData && isHasOwnProperty(componentData.interactionData.puzzleMap, 'matchLength')) {
      let puzzle = state.interactionData.puzzle;
      let matchLength = componentData.interactionData.puzzleMap.matchLength;
      let team_one = _.dropRight(puzzle, matchLength);
      let team_two = _.drop(puzzle, matchLength);

      let one = 0;
      let oneCount = 0;
      let two = 0;
      let twoCount = 0;
      for(let i = 0, len = team_one.length; i < len; i++) {
        one = parseInt(team_one[i]);
        two = parseInt(team_two[i]);
        if(one == 1) {
          oneCount++;
        }
        if(two == 1) {
          twoCount++;
        }
      }
      let count = _.min([oneCount, twoCount]);
      state.bindData.puzzle = count;
      componentData.commonAttr.itemIndex = count - 1 < 0 ? 0 : count - 1;
    }
    EventBus.$emit('refreshPuzzle');
  },
  updateDraw(state, {id, controlId, data}){
    //1383纯甄卡片抽奖
    let cardMap = null;
    if(!_.isArray(data) && data.ext_1){
      delete data.ext_1.NaN
      // data.ext_1 = {1: 5, 2: 5, 3: 5, 4: 5, 5: 5};
      cardMap = data.ext_1
      _.merge(state.controls[id][controlId].data, data);
      state.controls[id][controlId].data.cardMap = cardMap;
    }else {
      // {"card":{"1":0,"2":0,"3":0,"4":0,"5":0},"btnCompId":"cf6dde53-308c-4b3d-8d83-0a9fa6e412f5"}
      cardMap = state.componentMap[id].interactionData.cardMap.card;
      state.controls[id][controlId].data.cardMap = cardMap;
    }
    EventBus.$emit('refreshDraw');

    let compData = state.componentMap[id];
    let btnCompId = compData.interactionData.cardMap.btnCompId;
    if(btnCompId){
      let btnCompData = state.componentMap[btnCompId];
      let count = 0;
      _.forEach(cardMap, (value) => {
        if(value > 0){
          count++;
        }
      });
      let commonAttr = {filter: { isGrayscale: 0, grayscale: 5 }};
      let isGrey = true;
      if(count < _.size(cardMap)){
        commonAttr.filter.isGrayscale = true;
        isGrey = true;
      }else{
        commonAttr.filter.isGrayscale = false;
        isGrey = false;
      }
      _.merge(btnCompData.commonAttr, commonAttr);
      if(btnCompData.cid == COMPONENT_TYPES.wb_img) {
        EventBus.$emit('refreshImgStyles', { id: btnCompId });
      } else {
        EventBus.$emit('btnStatus', { isGrey, compId: btnCompId });
      }
    }
  },
  updateInitGeneral(state, {id, controlId, data}){
    if(state.controls[id][controlId].data.ele_key == GENERAL_TYPES_MAP.activity){
      let isTimeout = true
      if(!_.isEqual(state.controls[id][controlId].data, data) && data.ext_1 != 0){
        isTimeout = false
      }
      if(_.isArray(data)){
        isTimeout = false
      }
      if(isTimeout && state.popId > 0){
        if(state.pops_[popId-1].isReloadEvent) {
          state.siteInfo.reloadPopCb = window.setInterval(() => {
            if(state.siteInfo.isPopReload && !state.siteInfo.isLoading) {
              EventBus.$emit("reloadLoad", { popId: state.popId });
              state.siteInfo.isPopReload = false;
              window.clearInterval(state.siteInfo.reloadPopCb)
            }
          }, state.siteInfo.reloadTime);
        }
      }
      if(state.timeObj.isTimerEnd || (!state.timeObj.isTimerEnd && !state.timeObj.isTimerStart && data.ext_1 == 0)){
        data.ext_1 = -2
      }
      _.merge(state.controls[id][controlId].data, data)
    }else{
      _.merge(state.controls[id][controlId].data, data)
    }
    // coverPushCompPic(state.coverPushCompPic, state.controls);
  },
  updateJudgeRightWrong(state, {id, controlId, eles, right, row}){
    try{
      let elements = state.controls[id][controlId].data.elements
      while(elements.length > 0){
        elements.pop()
      }
      _.forEach(eles, (user) => {
        elements.push(user)
      })
      _.merge(state.controls[id][controlId].data.right, right)
      state.controls[id][controlId].data.row = row
    }catch(error){
      console.log(error)
    }
  },
  updateActivityInfo(state, { id, controlId, data }){
    if(id){
      let controlData = state.controls[id][controlId].data
      _.merge(state.activityInfo, controlData)
      _.merge(state.activityInfo, data)
      _.merge(controlData, data)
    }else{
      _.merge(state.activityInfo, data)
    }
    //通用计数 参加活动的用户 排名 isattend rank total
    // data = { rank: 3, total: 5000 }; 1743
    if(data.rank || data.total){
      state.bindData.generalRank = data.rank;
      state.bindData.generalTotal = data.total;
    }
    EventBus.$emit("refreshDynamicData");
  },
  updateGeneral(state, { id, controlId, data }){
    if(id){
      let controlData = state.controls[id][controlId].data
      controlData.isJoin = data.isJoin
      _.merge(state.activityInfo, controlData)
    }else{
      _.merge(state.activityInfo, data)
    }
    //通用计数 参加活动的用户 排名 isattend rank total
    // data = { rank: 3, total: 5000 }; 1743
    if(data.rank || data.total){
      state.bindData.generalRank = data.rank;
      state.bindData.generalTotal = data.total;
      EventBus.$emit("refreshDynamicData");
    }
  },
  updateComponentVisible(state, {id, commonAttr}){
    state.visibleComponents[id] = id
    let componentData = state.componentMap[id];
    if(componentData){
      _.merge(componentData.commonAttr, commonAttr)
    }
  },
  resetVisibleComponents_(state){
    resetVisibleComponents(state.visibleComponents, state.componentMap)
  },
  updateFileUploadData(state, data){
    let data_ = data.data;
    data_.key = data_.key.split("$")[0];
    state.fileUploadData = _.merge({ site_id: state.siteInfo.id, type: 1 }, data_);
  },
  updateLoginStatus(state, isLogin){
    state.userInfo.isLogin = isLogin
    localStorage.isLogin = state.userInfo.isLogin
  },
  updatePageIndex(state, {pageIndex}){

    if(state.coverPageCropCompAttr[pageIndex+1]){
      let compIds = state.coverPageCropCompAttr[pageIndex+1];
      let compId;
      while(compIds.length > 0){
        compId = compIds.pop();
        const compData = state.componentMap[compId];
        if(compData && compData.cid == COMPONENT_TYPES.wb_img){
          compData.commonAttr.url = compData.commonAttr.url_;
        }
      }
    }
    let oldPageIndex = state.pageIndex;
    state.pageIndex = pageIndex
    state.popId = -1

    if(pageIndex >= 0){
      resetVisibleComponents(state.visibleComponents, state.componentMap)
      //swiper 不用重置这些数据
      if(state.isLoadJsObj.isHtml2canvas && state.attrs.pageType == PAGE_TYPE.single && state.attrs.isNoSwiping){
        resetAllCropImgUrl(state.allPages);
      }
    }
    let body = document.getElementsByTagName("body")[0]
    if(body){
      body.style.backgroundColor = state.pages[state.pageIndex].styles.backgroundColor || state.styles.backgroundColor
    }
    // coverPushCompPic(state.coverPushCompPic, state.controls);
    if(state.pages[pageIndex].isReloadEvent) {
      state.siteInfo.reloadCb = window.setInterval(() => {
        if(state.siteInfo.isReload && !state.siteInfo.isLoading) {
          //这个需要足够的时间，不然页面还没有渲染完，事件无法监听到
          EventBus.$emit("reloadLoad", {pageId: pageIndex+1});
          state.siteInfo.isReload = false;
          if(pageIndex != 0 && pageIndex == oldPageIndex){
            EventBus.$emit("postInitEvent");
          }
          window.clearInterval(state.siteInfo.reloadCb)
        }
      }, state.siteInfo.reloadTime);
    }

    //恢复数据
    _.forEach(state.recoverCompMap, (comp, id) => {
      _.merge(state.componentMap[id].commonAttr, state.recoverCompMap[id]);
    })
  },
  updatePopId(state, {popId}){
    state.popId = popId
    if(popId > 0){
      // resetVisibleComponents(state.visibleComponents, state.componentMap);
      if(state.isLoadJsObj.isHtml2canvas){
        resetAllCropImgUrl(state.allPages);
      }
      // coverPushCompPic(state.coverPushCompPic, state.controls);
      if(state.pops_[popId-1].isReloadEvent) {
        state.siteInfo.reloadPopCb = window.setInterval(() => {
          if(state.siteInfo.isPopReload && !state.siteInfo.isLoading) {
            EventBus.$emit("reloadLoad", { popId: popId })
            state.siteInfo.isPopReload = false;
            window.clearInterval(state.siteInfo.reloadPopCb);
          }
        }, state.siteInfo.reloadTime);
      }
    }
  },
  updateLotteryForm(state, {id, controlId, data}){
    try{
      _.merge(state.controls[id][controlId].data, data)
    }catch(error){
      console.log(error)
    }
  },
  updateFormValue(state, data){
    let { id } = data //value item lists
    if(!state.formValues[id]){
      state.formValues[id] = {}
    }
    if(!_.isEqual(state.formValues[id], data)){
      state.siteInfo.formIsSubmit = true
    }
    state.formValues[id] = {
      ...data,
    };
  },
  updateControls(state, {id, controlId, data}) {
    try{
      _.merge(state.controls[id][controlId].data, data)
    }catch(error){
      console.log(error)
    }
  },
  setStatisticData(state, { id, controlId, data }){
    const compData = state.componentMap[id];
    const statisticDataType = compData.events.statisticData.comps[0].attrs.value;
    if(statisticDataType == STATISTIC_DATA_TYPES_MAP.weibo2020){
      data.opp_name = data.users[data.opp_uid] ? data.users[data.opp_uid].screen_name : "天神";
      let uids = [data.uid1_2019, data.uid2_2019, data.uid3_2019, data.uid1_2020, data.uid2_2020, data.uid3_2020];
      if(data.uid1_2019 || data.uid2_2019 || data.uid3_2019){
        data.friends = data.uid1_2019 || data.uid2_2019 || data.uid3_2019
      }else{
        data.friends = ''
      }
      data.elements = [];
      _.forEach(uids, (uid) => {
        data.elements.push(data.users[uid]);
      })
    }
    if(statisticDataType == STATISTIC_DATA_TYPES_MAP.weiboTime){
      _.forEach(data, (value,key) =>{
        if(key.indexOf('cnt') > -1){
          data[key] = Number(value)
        }
      })
      data.pub_all_cnt =  data.pub_origin_count_cnt + data.pub_transmit_cnt
    }
    if(statisticDataType == STATISTIC_DATA_TYPES_MAP.aes){
      state.controls[id][controlId].data = data;
      state.statisticData = state.controls[id][controlId].data;
      dealStatisticData(statisticDataType, state, compData);
    }else if(statisticDataType == STATISTIC_DATA_TYPES_MAP.xiaomi10Days || statisticDataType == STATISTIC_DATA_TYPES_MAP.xiaomi10Interact){
      if(!_.isArray(data)){
        _.merge(state.controls[id][controlId].data, data);
        _.merge(state.statisticData, state.controls[id][controlId].data);
      }
      if(statisticDataType == STATISTIC_DATA_TYPES_MAP.xiaomi10Interact){
        if(!state.statisticData['founder_date']){
          state.statisticData.founder_date = ''
        }
        if(!state.statisticData['xiaomi_date']){
          state.statisticData['xiaomi_date'] = ''
        }
      }

      if(state.statisticData['reister_days'] && isHasOwnProperty(state.statisticData, 'founder_date')){
        dealStatisticData(statisticDataType, state, compData);
      }
    }else if(statisticDataType == STATISTIC_DATA_TYPES_MAP.weibo2021vote){
      state.controls[id][controlId].data = data;
      state.bindData.uid = data.sn
      state.bindData.workNum = data.ext2
      state.bindData.mid = data.ext3
      state.bindData.name = data.ext4
      if(data.user){
        state.shareData.url = data.user.avatar_large
        state.bindData.nickName = data.user.screen_name
      }
      state.shareData.midJump = data.ext3 && data.ext3.length > 0 ? "sinaweibo://detail/?mblogid=" + data.ext3 : data.ext1
      EventBus.$emit("refreshDynamicData")
    }else{
      _.merge(state.controls[id][controlId].data, data);
      state.statisticData = state.controls[id][controlId].data;
      dealStatisticData(statisticDataType, state, compData);
      if(statisticDataType == STATISTIC_DATA_TYPES_MAP.weibo2020 || statisticDataType == STATISTIC_DATA_TYPES_MAP.weiboTime ){
        EventBus.$emit("refreshDynamicData")
      }
    }
  },
  setUserUid (state, data) {
    if(data.data.length > 0){
      state.userInfo.isLogin = true
      state.userInfo.uid = data.data
    }else{
      state.userInfo.isLogin = false
      state.userInfo.uid = null
    }
    localStorage.isLogin = state.userInfo.isLogin
    localStorage.uid = state.userInfo.uid
  },
  setWbUserInfo (state, { data }) {
    //id, controlId,
    state.userInfo.wbUserData = data
    state.userInfo.isWbUserData = false
    state.userInfo.isLogin = true
    state.userInfo.uid = state.userInfo.wbUserData.id

    localStorage.isLogin = state.userInfo.isLogin
    localStorage.uid = state.userInfo.uid
  },
  setUserList(state, {id, controlId, data}){
    try{
      if(!data){
        return
      }
      let elements = state.controls[id][controlId].data.elements

      if(data.users && _.isArray(data.users)){
        state.controls[id][controlId].data.oriElements = data.users;
        let elements_ = {};
        _.forEach(elements, (item) => {
          elements_[item.uid] = item;
        })
        _.forEach(data.users, (user, index) => {
          if(elements[index]){
            if(elements_[user.id]){
              elements[index] = elements_[user.id]
              _.merge(elements[index], user)
            }else{
              elements[index] = user;
            }
          }else{
            elements.push(user)
          }
        })
      }else{
        if(elements[0]){
          for(let key in data){
            //Vue.set
            elements[0][key] = data[key]
          }
        }
      }
      let componentData = state.componentMap[id];
      let controlData = state.controls[id]
      sortReposts(controlData, componentData)
      EventBus.$emit("refreshDynamicData")
      EventBus.$emit("refreshFriends", { id })
    }catch(error){
      console.log(error)
    }
  },
  setWbList (state, {id, controlId, data}) {
    try{
      _.merge(state.controls[id][controlId].data, data)
    }catch(error){
      console.log(error)
    }
  },
  setWbNumber(state, {id, controlId, data}){
    try{
      if(_.isArray(data)){
        return
      }
      if(data.num >= 0){
        _.merge(state.controls[id][controlId].data, data)
        initNumberDataProcess(state.controls[id][controlId].data, id, state.componentMap, state.controls, state.siteInfo);
        refreshBitmapTextFrame(id, data.num, controlId, state.componentMap)
        //前端实时改变按钮状态
        if(isHasOwnProperty(state.controls[id][controlId].data, 'dayNum')){
          state.controls[id][controlId].data.dayNum++;
        }
      }
      if(data.syn_com_id_datas && !_.isArray(data.syn_com_id_datas)){
        let syn_com_id_str = data.syn_com_id
        if(syn_com_id_str && syn_com_id_str.length > 0){
          const syn_com_id = getCompIdByParam(syn_com_id_str)
          if(syn_com_id){
            let syn_com_id_data = state.controls[syn_com_id][controlId].data
            syn_com_id_data.num = data.syn_com_id_datas.num
          }
        }
      }
      EventBus.$emit("refreshDynamicData")
    }catch(error){
      console.log(error)
    }
  },
  setWbInitNumber(state, {id, controlId, data}){
    try{
      _.merge(state.controls[id][controlId].data, data)
      initNumberDataProcess(state.controls[id][controlId].data, id, state.componentMap, state.controls, state.siteInfo);
      refreshBitmapTextFrame(id, data.num, controlId, state.componentMap)
      EventBus.$emit("refreshDynamicData");
    }catch(error){
      console.log(error)
    }
  },
  setNumberDayExists(state, {id, controlId, data}){
    try{
      let numberControlData = state.controls[id][CONTROL_TYPES.number];
      let wbData = state.controls[id][controlId].data;
      if(_.isBoolean(data)) {
        wbData.isNumber = data ? false : true
      } else {
        wbData.isNumber = parseInt(data)!= 0 && parseInt(data) >= parseInt(numberControlData.data.day_limit) ? false : true;
        numberControlData.data.dayNum = parseInt(data);
      }
      EventBus.$emit("refreshNumberDayExists", {compId:getCompIdByParam(wbData.relateCompId), isNumber: wbData.isNumber});
    }catch(error){
      console.log(error)
    }
  },
  setReposts(state, {id, controlId, data}){
    try{
      //init的接口是并行的，vote接口后发先到，这里是处理这种情况的
      let data_ = {}
      _.forEach(data, (item) => {
        data_[item.mid] = item
      })
      let elements = state.controls[id][controlId].data.elements
      _.forEach(elements, (elementItem) => {
        let searchItem = data_[elementItem.mid]
        _.merge(elementItem, searchItem)
      })

      let componentData = state.componentMap[id];
      let controlData = state.controls[id]
      sortReposts(controlData, componentData)
      EventBus.$emit("refreshDynamicData")
    }catch(error){
      console.log(error)
    }
  },
  updateRepostsFollow(state, {id, controlId, mid}){
    try{
      if(!state.controls[id]){
        return
      }
      let elements = state.controls[id][controlId].data.elements
      if(elements){
        let item
        for(let i = 0, len = elements.length; i < len; i++){
          item = elements[i]
          if(item.mid == mid){
            item.user.followers_count += 1
            break
          }
        }
      }
    }catch(error){
      console.log(error)
    }
  },
  updateRepostsPraise(state, {id, controlId, mid}){
    try{
      let elements = state.controls[id][controlId].data.elements
      let item
      for(let i = 0, len = elements.length; i < len; i++){
        item = elements[i]
        if(item.mid == mid){
          item.attitudes_count += 1
          break
        }
      }
    }catch(error){
      console.log(error)
    }
  },
  setShare(state, data){
    state.siteInfo.wxTicket = data.data;
  },
  setWbInitVote(state, {id, controlId, data}){
    try{
      if(data.list){
        _.merge(state.controls[id][controlId].data.elements, data.list)
      }else{
        _.merge(state.controls[id][controlId].data.elements, data)
      }
      if(isHasOwnProperty(data, 'remain')){
        state.bindData.remain = parseInt(data.remain)
      }
      let elements = state.controls[id][controlId].data.elements
      let total = computeVoteTotal(elements);
      let vote_coefficient = parseInt(state.controls[id][controlId].data.vote_coefficient);
      if(!vote_coefficient){
        vote_coefficient = 1;
      }
      state.bindData.total = total * vote_coefficient;

      let componentData = state.componentMap[id];
      let controlData = state.controls[id]
      sortReposts(controlData, componentData)

      EventBus.$emit("refreshDynamicData")
    }catch(error){
      console.log(error)
    }
  },
  setWbVoteNum(state, {id, controlId, data}){
    try{
      const syn_vote_id_datas = data.syn_vote_id_datas;
      const remain = data.remain;
      delete data.syn_vote_id_datas;
      delete data.remain;

      _.merge(state.controls[id][controlId].data.elements, data)
      const elements = state.controls[id][controlId].data.elements

      let total = computeVoteTotal(elements);
      let vote_coefficient = parseInt(state.controls[id][controlId].data.vote_coefficient);
      if(!vote_coefficient){
        vote_coefficient = 1;
      }
      state.bindData.total = total * vote_coefficient;

      if(remain >= 0){
        state.bindData.remain = remain
      }

      const componentData = state.componentMap[id];
      const controlData = state.controls[id]
      sortReposts(controlData, componentData)

      if(syn_vote_id_datas && !_.isArray(syn_vote_id_datas)){
        let syn_com_id = state.controls[id][controlId].data.syn_com_id
        if(syn_com_id && syn_com_id.length > 0){
          let arr = syn_com_id.split("$")
          if(arr.length > 1){
            syn_com_id = arr[1]
            let syn_com_id_datas = state.controls[syn_com_id][controlId].data.elements
            _.forEach(syn_vote_id_datas, (itemData, mid) => {
              _.merge(syn_com_id_datas[mid], itemData)
            })
          }
        }
      }
      EventBus.$emit("refreshDynamicData")
    }catch(error){
      console.log(error)
    }
  },
  setQrcode(state, data){
    state.qrcode = data.qrcode
  },
  updateShareData(state, data){
    let shareData = state.shareData;
    _.forEach(shareData, (value, key) => {
      if(_.isArray(value)){
        shareData[key] = [];
      }else{
        shareData[key] = '';
      }
    })
    _.merge(shareData, data);
  },
  updateInteractionData(state, data){
    _.merge(state.interactionData, data);
  },
  resetInteractionData(state){
    state.interactionData = _.cloneDeep(INTERACTION_CLICK_DATA);
  },
  updateArea(state, { type, data, id, controlId}){
    if(type == GENERAL_TYPES_MAP.area){
      let elements = [];
      let wbData = state.controls[id][controlId].data;
      wbData.area_code = data.area_code;
      wbData.area = data.area;
      _.forEach(data.area, (name, key) => {
        elements.push({ name, value: key })
      })
      wbData.elements = elements;
    }
  },
  updateAreaRank(state, { type, data, id, controlId}){
    if(type == GENERAL_TYPES_MAP.areaRank){
      let wbData = state.controls[id][controlId].data;
      let elements = [];
      let rank = 1;
      _.forEach(data.area, (value, name) => {
        elements.push({ rank, area: name, num: value });
        rank++;
      });
      elements = _.sortBy(elements, (item) => { return -item.num; });
      _.forEach(elements, (item, index) => {
        item.rank = index + 1
      })
      wbData.elements = elements;
      state.bindData.total = data.total;
      EventBus.$emit("refreshDynamicData")
    }
  },
  updateBindData(state, {key, value}){
    state.bindData[key] = value;
    EventBus.$emit("refreshDynamicData");
  },
  addNeedRecoverCompId(state, componentData){
    if(!state.recoverCompMap[componentData.id]) {
      state.recoverCompMap[componentData.id] = _.cloneDeep(componentData.commonAttr);
    }
  },
  setFriends(state, {id, controlId, data}){
    try{
      //rankType
      let elements = state.controls[id][controlId].data.elements
      while(elements.length > 0){
        elements.pop()
      }
      _.forEach(data, (user) => {
        elements.push(user)
        user.num = parseInt(user.num);
      })
      EventBus.$emit("refreshFriends", { id })
      EventBus.$emit("refreshDynamicData")
    }catch(error){
      console.log(error)
    }
  },
  //后台过来的数据，需要处理为合适各个平台的，还有一些全屏等渲染页面需要的属性需要设置
  setPageData (state, data) {
    if(!data || !data.pages){
      Dialog.alert({
        title: '',
        message: '站点数据错误',
      })
      return
    }
    if(isHasOwnProperty(data,'uid') && data.uid && data.uid.length > 0){
      state.userInfo.uid = data.uid
      state.userInfo.isLogin = true
    }
    state.siteInfo.isTemplate = data.is_template == "1" ? true : false

    dealSiteType(data);
    MOBILE_WID_HEI.adaptiveScale = MOBILE_WID_HEI.width/MOBILE_WID_HEI.clientWidth

    if(data.attrs.pageType == PAGE_TYPE.long || data.attrs.isNoSwiping){
      state.isLoadJsObj.isSwiper =  false
    }else{
      state.isLoadJsObj.isSwiper =  true
    }
    state.styles = data.styles
    state.attrs = data.attrs
    let popId = parseInt(getQueryString('popId'));
    if(popId){
      state.attrs.popId = popId;
    }
    let pageId = getQueryString('pageId');
    if(pageId){
      state.attrs.pageId = pageId;
    }

    state.controls = data.controls ? data.controls : {}

    state.siteInfo.isDisplayAssetsLoading = data.attrs.isDisplayAssetsLoading
    state.siteInfo.id = parseInt(data.id)
    if(data.attrs.relateSiteId && data.attrs.relateSiteId.length > 0 && _.isNumber(parseInt(data.attrs.relateSiteId))){
      state.siteInfo.id = data.attrs.relateSiteId
    }

    for(let i = 0, pageLen = data.pages.length; i < pageLen; i++){
      let page = data.pages[i]
      page.attrs.playAnimateAllNum = 0;

      //页面中选中图片的可见性
      let selectedCompId = page.attrs.selectedCompId
      if(selectedCompId && selectedCompId.length > 0){
        setCompnentDataIsVisible(selectedCompId, state.componentMap)
      }

      dealPageStyleAndAttr(data.attrs, page)

      dealPageOrGroup(state, data, page, page, false, data.pages);

      if(i == 0 && state.fixedComps.length > 0){
        _.pullAllWith(page.components, state.fixedComps, _.isEqual);
      }
      if(page.isRelative) {
        delete page.styles.height
      }
    }
    EventBus.$emit("completeLoadSiteData", data)
    postStatics(state, {comType: "visit", wModule:"visit"})
  },
  setSitePages(state, data){
    const pops = _.filter(data.pages, function(o) { return o.type == PAGE_TYPE_.pop })
    let allPages = state.allPages = data.pages
    _.forEach(allPages, (page) => {
      state.allPagesMap[page.id] = page
    })
    state.pages = _.filter(data.pages, function(o) { return o.type == PAGE_TYPE_.page })
    state.pops_ = pops

    state.siteInfo.isInitSite = false

    if(data.attrs.reloadTime) {
      state.siteInfo.reloadTime = parseInt(data.attrs.reloadTime)
    }
  }
}
